package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorimpl implements ApReadBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        Date date = new Date();
        long statictime = date.getTime();
        ApUser user = AppThreadLocalUtils.getUser();
        //根据用户查询
        ApBehaviorEntry userIdOrEquipmentId = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        String id = userIdOrEquipmentId.getId();
        System.out.println(id);
        System.out.println(userIdOrEquipmentId);
//如果不登陆,直接返回结果
        if (id == null) {
            return ResponseResult.okResult();
        }
        ApReadBehavior behavior = new ApReadBehavior();
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()));

        ApReadBehavior readBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
        //根据条件查询,如果没有则添加
        if (readBehavior == null) {
            behavior.setCount((short) 1);
            behavior.setCreatedTime(new Date());
            behavior.setArticleId(dto.getArticleId());
            behavior.setEntryId(id);
            long endtime = date.getTime();
            long data = statictime - endtime;
            behavior.setReadDuration((int) data);
            mongoTemplate.save(behavior);
        } else {
            //获取当前查看次数
            Short count = readBehavior.getCount();
            count++;
            Update update = new Update();
            //根据条件
            update.set("count", count);
            mongoTemplate.updateMulti(query, update, ApReadBehavior.class);
        }
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("阅读行为 发送消息 ==>{}",mess);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
